﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;
using MSIACCSL2.Web.WarehouseServices;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class DO : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument DeliveryOrder;
        private ReportDocument SuratJalan;
        private string Tipe;

        public DO()
        {
            DA = new DataAccess<object>();
            DeliveryOrder = new ReportDocument();
            SuratJalan = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
            if (IsPostBack)
                if (RadioButtonList2.SelectedValue.ToString().Contains("Packing"))
                    LoadReportPackingList(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
                else
                    LoadReportSuratJalan(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
        }
      
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (RadioButtonList2.SelectedValue.ToString().Contains("Packing"))
                LoadReportPackingList(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
            else
                LoadReportSuratJalan(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);

        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
        }


        public void LoadReportPackingList(string  NoDO,string tipe)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Select a.*,i.SerialNumber,i.NoBundle,i.QtyPack, ");
            sb.Append("i.KodeMasuk,iw.KodePallet,i.SatuanPack,i.SatuanUnit, ");
            sb.Append("c.*,it.ItemName,s.SubGroupName,iw.KodeWarehouse, ");
            sb.Append("b.Panjang,b.Lebar,b.Tinggi,b.QtyUnit ");
            sb.Append("from DeliveryOrder a  ");
            sb.Append("inner Join DeliveryOrderDetail b on a.DONo = b.DONo  ");
            sb.Append("LEFT JOIN inventory i ON i.SerialNumber = b.NoLog  ");
            sb.Append("Left Join Customer c on c.CustomerId = a.CustomerId   ");
            sb.Append("Left Join Item it on it.ItemId = i.ItemId   ");
            sb.Append("Left Join Subgroup s on s.SubGroupID = it.SubgroupId ");
            sb.Append("Left JOIN (Select SerialNumber,KodeWarehouse,Pallet AS KodePallet from uvw_mutasi_stok) iw ON i.SerialNumber = iw.SerialNumber AND iw.KodeWarehouse=a.KodeWarehouse ");
            sb.AppendFormat("Where a.DONo='{0}'  Order By s.SubgroupId,i.ItemId   ", NoDO);

            var Source = DA.GetDataListDataSet(sb.ToString());
            string reportPath = Server.MapPath("~/Report/DeliveryOrder.rpt");
            DeliveryOrder.Load(reportPath);
            DeliveryOrder.SetDataSource(Source.Tables[0]);
            var param = new ParameterDiscreteValue();
            param.Value = tipe;
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            DeliveryOrder.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
            CrystalReportViewer1.ReportSource = DeliveryOrder;
        }

        public void LoadReportSuratJalan(string NoDO,string tipe)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Select a.*,i.SerialNumber,i.NoBundle,i.QtyPack, ");
            sb.Append("i.KodeMasuk,i.KodePallet,i.SatuanPack,i.SatuanUnit, ");
            sb.Append("c.*,it.ItemName,s.SubGroupName,iw.KodeWarehouse, ");
            sb.Append("b.Panjang,b.Lebar,b.Tinggi,b.QtyUnit ");
            sb.Append("from DeliveryOrder a  ");
            sb.Append("inner Join DeliveryOrderDetail b on a.DONo = b.DONo  ");
            sb.Append("LEFT JOIN inventory i ON i.SerialNumber = b.NoLog  ");
            sb.Append("Left Join Customer c on c.CustomerId = a.CustomerId   ");
            sb.Append("Left Join Item it on it.ItemId = i.ItemId   ");
            sb.Append("Left Join Subgroup s on s.SubGroupID = it.SubgroupId ");
            sb.Append("Left JOIN uvw_mutasi_stok iw ON i.SerialNumber = iw.SerialNumber AND iw.KodeWarehouse = a.KodeWarehouse ");
            sb.AppendFormat("Where a.DONo='{0}'  Order By i.ItemId  ", NoDO);

          
            var Source = DA.GetDataListDataSet(sb.ToString());
            string reportPath = Server.MapPath("~/Report/SuratJalan.rpt");
            SuratJalan.Load(reportPath);
            SuratJalan.SetDataSource(Source.Tables[0]);
            var param = new ParameterDiscreteValue();
            param.Value = tipe;
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            SuratJalan.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
            CrystalReportViewer1.ReportSource = SuratJalan;
        }


        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            DeliveryOrder.Clone();
            DeliveryOrder.Dispose();

            SuratJalan.Clone();
            SuratJalan.Dispose();
        }
    }
}